export const state = () => ({
    userInfo: {
        token: "",
        user: {}
    }
})

export const mutations = {
    setUserInfo(state, data) {
        console.log(data);
        state.userInfo.token = data.token
        state.userInfo.user = data.user
            // localStorage.setItem("userInfo", JSON.stringify(state))
    },
    clearUserInfo(state) {
        state.userInfo = {
            token: "",
            user: {}
        }
    }
}
export const actions = {
    login({ commit }, data) {
        return this.$axios.post('/api2/user/login', data).then(res => {
            let r = res.data
            if (r.type == 'SUCCESS_LOGIN') {
                commit('setUserInfo', r)
                Promise.resolve()
            }
        })
    },
    refresh({ commit }, data) {
        return this.$axios.get('/api2/user/refresh?token=' + data).then(res => {
            let r = res.data
            if (r.type == 'SUCCESS_LOGIN') {
                commit('setUserInfo', r)
                Promise.resolve()
            }
        })
    },

}